package com.jzwl.site.wap.index.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jzwl.common.constant.Constants;
import com.jzwl.common.id.Sequence;
import com.jzwl.common.page.PageObject;
import com.jzwl.system.base.dao.BaseDAO;

@Repository("webDao")
public class WebDao {

	@Autowired
	private BaseDAO baseDAO;

	@Autowired
	private Constants constants;

	public List<Map<String, Object>> queryStaticInfo(String type) {
		String sql = "SELECT * from " + constants.getCurrentDB() + "site_carousel_v2 t";
		return baseDAO.queryForList(sql);
	}

	public Map<String, Object> queryTopNode() {
		String sql = "select sn.id,sn.name,sn.parentId from "
				+ constants.getCurrentDB()
				+ "site_node sn left join "
				+ constants.getCurrentDB()
				+ " site_node_type snt on sn.typeId=snt.id where sn.isDelete=0 and snt.name='资讯' order by sn.ord desc limit 1";
		return baseDAO.queryForMap(sql);
	}

	public List queryNode() {
		String sql = "SELECT * FROM site_node t  ORDER BY t.`createTime` ASC;";
		return baseDAO.queryForList(sql);
	}

	public List queryNodeType() {
		String sql = "select id,name from " + constants.getCurrentDB()
				+ "site_node_type where isDelete=0  order by ord desc,id";
		return baseDAO.queryForList(sql);
	}

	public List queryTopNodeByType(Object type) {
		String sql = "select id,name,parentId from " + constants.getCurrentDB() + "site_node where typeId=" + type
				+ " and parentId !=0 and isDelete=0 order by ord desc limit 2";
		return baseDAO.queryForList(sql);
	}

	public List queryTopArticle() {
		String sql = "select articleId,title,picLink,nodeId from " + constants.getCurrentDB()
				+ "site_article where isRecom=1 and isDelete=0 and isPub=1 order by createTime desc";
		return baseDAO.queryForList(sql);
	}

	public List queryTopArticleByNode(Object nodeid) {
		String sql = "select articleId,nodeId,title,picLink from " + constants.getCurrentDB()
				+ "site_article where nodeId = " + nodeid
				+ " and isDelete=0 and isPub=1 order by isTop desc, createTime desc limit 10";
		return baseDAO.queryForList(sql);
	}

	public List queryTopArticleByCreatTime(Object nodeid) {
		String sql = "select articleId,nodeId,title,picLink,content,createTime,pubTime from "
				+ constants.getCurrentDB() + "site_article where isDelete=0 and isPub=1 and nodeId=" + nodeid
				+ " ORDER BY pubTime desc, createTime desc limit 10";

		return baseDAO.queryForList(sql);
	}

	/**
	 * 根据导航的id获取文章的内容
	 * @Date 2018年9月7日
	 * @author gyp
	 * @param nodeid
	 * @return(这里描述每个参数,如果有返回值描述返回值,如果有异常描述异常)
	 */
	public List<Map<String, Object>> getRecomdArticleList(Object nodeid) {
		String sql = "select t.articleId,t.nodeId, t.title, t.subtitle,t.content, t.picLink, t.author, t.pubTime,  t.readCount, t.isRecom, t.isTop, t.isPub, t.createTime  from "
				+ constants.getCurrentDB()
				+ "site_article t where t.`isRecom` = 1 and t.isPub = 1 and  t.isDelete=0 and t.nodeId="
				+ nodeid
				+ " ORDER BY pubTime desc, createTime desc limit 3";
		return baseDAO.queryForList(sql);
	}

	/**
	 * 1-获取一级导航
	 * @Date 2018年9月6日
	 * @author gyp
	 * @return(这里描述每个参数,如果有返回值描述返回值,如果有异常描述异常)
	 */
	public List<Map<String, Object>> queryFirstMenu() {
		String sql = "select id,name,parentId,isRedirect,redirectUrl,isDisplay from " + constants.getCurrentDB()
				+ "site_node where  parentId =0 and isDelete=0 and isDisplay=1 order by ord asc ";

		return baseDAO.queryForList(sql);
	}

	public Map<String, Object> queryDataValue() {

		String sql = "SELECT dd.id,dd.dic_name,dd.dic_value as dicValue from v2_dic_type  dt  LEFT JOIN  v2_dic_data dd on dt.dic_id = dd.dic_id where  dd.isDelete = 0 and dt.dic_code='jkgljg'";
		return baseDAO.queryForMap(sql);
	}

	/**
	 * 1-根据父级节点的id获取所有的子节点
	 * @Date 2018年9月6日
	 * @author gyp
	 * @param id
	 * @return(这里描述每个参数,如果有返回值描述返回值,如果有异常描述异常)
	 */
	public List querySecondMenu(Object id) {
		/*String sql = "select id,name,parentId,isRedirect,redirectUrl,isDisplay from " + constants.getCurrentDB() + "site_node where  parentId =0 and isDelete=0 and isDisplay=1 order by ord desc limit 5";*/
		String sql = "select id,name,parentId,isRedirect,redirectUrl,isDisplay from " + constants.getCurrentDB()
				+ "site_node where  parentId =" + id + " and isDelete=0 order by ord asc ";

		return baseDAO.queryForList(sql);
	}

	public Map queryNodeInfo(Object nodeid) {
		String sql = "select t.id,t.`name`,t.parentId,t1.`name` as typeName from " + constants.getCurrentDB()
				+ "site_node t LEFT JOIN site_node_type t1 ON t.typeId = t1.id where t.id =" + nodeid
				+ " and t.isDelete=0 order by t.ord desc";
		return baseDAO.queryForMap(sql);
	}

	/**
	 * 根据id获取节点的信息
	 * @Date 2018年9月11日
	 * @author gyp
	 * @param nodeid
	 * @return(这里描述每个参数,如果有返回值描述返回值,如果有异常描述异常)
	 */
	public Map queryNodeInfoById(Object nodeid) {
		String sql = "select * from " + constants.getCurrentDB() + "site_node t where t.id =" + nodeid
				+ " and t.isDelete=0 order by t.ord desc";
		return baseDAO.queryForMap(sql);
	}

	public List queryNodeBypid(Object pid) {
		String sql = "select id,name,parentId from " + constants.getCurrentDB() + "site_node where parentId =" + pid
				+ " and isDelete=0 order by ord desc";
		return baseDAO.queryForList(sql);
	}

	/**
	 * 根据导航的id获取文章
	 * (这里用一句话描述这个方法的作用)
	 * @Date 2018年9月6日
	 * @author gyp
	 * @param map
	 * @return(这里描述每个参数,如果有返回值描述返回值,如果有异常描述异常)
	 */
	public PageObject queryArticleListByNodeId(Map<String, Object> map) {
		int currentPage = Integer.parseInt(map.get("start").toString());
		String sql = "";
		if (null == map.get("serchValue")) {
			sql = "select t.articleId,t.nodeId, t.title, t.subtitle,t.content, t.picLink, t.author, t.pubTime,  t.readCount, t.isRecom, t.isTop, t.isPub, t.createTime from "
					+ constants.getCurrentDB()
					+ "site_article t "
					+ "where nodeId = "
					+ map.get("menuId")
					+ " and isDelete=0 AND t.`articleType` = 1 and t.isPub=1  order by pubTime desc";
		} else {
			sql = "select t.articleId,t.nodeId, t.title, t.subtitle,t.content, t.picLink, t.author, t.pubTime,  t.readCount, t.isRecom, t.isTop, t.isPub, t.createTime from "
					+ constants.getCurrentDB()
					+ "site_article t "
					+ "where nodeId = "
					+ map.get("menuId")
					+ " and t.title like '%"
					+ map.get("serchValue")
					+ "%' and isDelete=0 AND t.`articleType` = 1 and t.isPub=1  order by pubTime desc";
		}
		PageObject po = baseDAO.queryForPageList(sql, currentPage);
		return po;
	}

	public Map<String, Object> queryArticleById(String aid) {
		String sql = "select t.articleId,t.nodeId, t.title, t.subtitle,t.content, t.picLink, t.author, t.pubTime,  t.readCount, t.isRecom, t.isTop, t.isPub, t.createTime from "
				+ constants.getCurrentDB()
				+ "site_article "
				+ "where articleId = "
				+ aid
				+ " and isDelete=0 and isPub=1 ";
		return baseDAO.queryForMap(sql);
	}

	public Map<String, Object> queryUsInfo() {
		String sql = "select content from " + constants.getCurrentDB() + "site_article " + "where articleId =0 ";
		return baseDAO.queryForMap(sql);
	}

	public boolean isExist(Map<String, Object> map) {
		String sql = "select count(*) as sum from " + constants.getCurrentDB() + "site_article_click "
				+ "where articleId =" + map.get("articleId") + " and ip='" + map.get("ip") + "'";
		Map rmap = baseDAO.queryForMap(sql);
		if (Integer.parseInt(rmap.get("sum").toString()) > 0) {
			return true;
		} else {
			return false;
		}
	}

	public boolean addClickInfo(Map<String, Object> map) {
		map.put("time", new Date());

		String sql = "insert into " + constants.getCurrentDB() + "site_article_click " + " (articleId,ip,time) "
				+ " values " + " (:articleId,:ip,:time)";
		if (baseDAO.executeNamedCommand(sql, map)) {
			return addClickNum(map);
		}
		return false;
	}

	public boolean addClickNum(Map<String, Object> map) {

		String sql = "update " + constants.getCurrentDB() + "site_article " + " set readCount =  readCount + 1"
				+ " where articleId =" + map.get("articleId");

		return baseDAO.executeNamedCommand(sql, map);
	}

	/**
	 * 
	 * 项目名称：sftj-offical-site
	 * 描述：添加用户信息
	 * 创建人： ln
	 * 创建时间： 2016年11月30日
	 * 标记：
	 * @param paramsMap
	 * @return
	 * @version
	 */
	public boolean addUserIndex(Map<String, Object> map) {
		map.put("id", Sequence.nextId());
		if (null == map.get("cooperationNeeds")) {
			map.put("cooperationNeeds", "");
		}
		String sql = "insert into `xiaoka-xydk`.user_index " + " (id,userName,phone,cooperationNeeds) " + " values "
				+ " (:id,:userName,:phone,:cooperationNeeds)";
		return baseDAO.executeNamedCommand(sql, map);

	}

	/**
	 * 
	 * 项目名称：sftj-offical-site
	 * 描述：校验用户信息
	 * 创建人： ln
	 * 创建时间： 2016年12月2日
	 * 标记：
	 * @param paramsMap
	 * @return
	 * @version
	 */
	public boolean checkUser(Map<String, Object> map) {

		String sql = " select id from `xiaoka-xydk`.user_index where userName='" + map.get("userName")
				+ "' and phone = '" + map.get("phone") + "' " + " and cooperationNeeds = '"
				+ map.get("cooperationNeeds") + "'";
		Map<String, Object> qMap = baseDAO.queryForMap(sql);
		if (null != qMap.get("id")) {
			return true;//存在
		}
		return false;
	}

	/**
	 * 
	 * 项目名称：sftj-offical-site
	 * 描述：新闻列表，用于手机端入口。H5
	 * 创建人： ln
	 * 创建时间： 2017年1月6日
	 * 标记：
	 * @param request
	 * @param response
	 * @return
	 * @version
	 */
	public PageObject queryNewList(Map<String, Object> map) {
		try {
			String sql = " SELECT t.articleId, t.nodeId, t.title, t.content, t.picLink, t.author, t.pubTime, t.readCount, t.isRecom, t.isTop, t.isPub, t.createTime "
					+ " FROM site_article t "
					+ " WHERE 1 = 1 "
					+ " AND t.nodeId =  "
					+ map.get("nodeId")
					+ ""
					+ " AND t.isDelete = 0 " + " ORDER BY isTop desc ,t.pubTime DESC";
			PageObject po = baseDAO.queryForMPageList(sql, new Object[] {}, map);
			return po;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 1-获取导航首页的推荐内容
	 * @Date 2018年9月7日
	 * @author gyp
	 * @param map
	 * @return(这里描述每个参数,如果有返回值描述返回值,如果有异常描述异常)
	 */
	public Map<String, Object> getRecomdContent(String nodeId) {
		try {
			String sql = " SELECT t.articleId,t.nodeId, t.title, t.subtitle,t.content, t.picLink, t.author, t.pubTime,  t.readCount, t.isRecom, t.isTop, t.isPub, t.createTime FROM site_article t WHERE t.`nodeId` = '"
					+ nodeId + "' AND t.`articleType` = 2 ";
			return baseDAO.queryForMap(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 1-获取文章的详情
	 * @Date 2018年9月7日
	 * @author gyp
	 * @param map
	 * @return(这里描述每个参数,如果有返回值描述返回值,如果有异常描述异常)
	 */
	public Map<String, Object> getArticleDetail(String articleId) {
		try {
			String sql = " SELECT t.articleId,t.nodeId, t.title, t.subtitle,t.content, t.picLink, t.author, t.pubTime,  t.readCount, t.isRecom, t.isTop, t.isPub, t.createTime "
					+ " FROM site_article t "
					+ " WHERE 1 = 1 "
					+ " AND t.articleId =  "
					+ articleId
					+ ""
					+ " AND t.isDelete = 0 ";
			return baseDAO.queryForMap(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * 项目名称：sftj-offical-site
	 * 描述：阅读人数+1
	 * 创建人： ln
	 * 创建时间： 2017年1月6日
	 * 标记：wap
	 * @param paramsMap
	 * @version
	 */
	public void updateReadNum(Map<String, Object> map) {
		String sql = "update " + constants.getCurrentDB() + "site_article " + " set readCount =  readCount + 1"
				+ " where articleId =" + map.get("id");
		boolean flag = baseDAO.executeNamedCommand(sql, map);
	}

	/**
	 * 
	 * 项目名称：sftj-offical-site
	 * 描述：获取置顶内容
	 * 创建人： ln
	 * 创建时间： 2017年1月6日
	 * 标记：
	 * @return
	 * @version
	 */
	public List<Map<String, Object>> queryNewListTop(Map<String, Object> map) {
		try {
			String sql = " SELECT t.articleId, t.nodeId, t.title, t.content, t.picLink, t.author, t.pubTime, t.readCount, t.isRecom, t.isTop, t.isPub, t.createTime "
					+ " FROM site_article t "
					+ " WHERE 1 = 1 and t.isTop = 1 "
					+ " AND t.nodeId = "
					+ map.get("nodeId") + "" + " AND t.isDelete = 0 " + " ORDER BY t.pubTime desc ";
			return baseDAO.queryForList(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
